
import SnapKit
import UIKit

class BLPlayerViewControllerTool : UIViewController {
    // 单例对象
    static let shareInstance: BLPlayerViewControllerTool = BLPlayerViewControllerTool()
    /* 播放器 */
    var player : IJKMediaPlayback? = nil
    /* 定时器, 用作更新播放进度 */
    var progressTimer = NSTimer()
    /* 播放器承载View */
    var displayView = UIView()
    var isPlay : Bool = false
    
    var playerView :UIView?
    
    
    
}
extension BLPlayerViewControllerTool{
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        player?.prepareToPlay()
    }
    
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        self.player?.shutdown()
        player = nil

    }
    
}


/// 配置IJKFFMoviePlayerController 信息
extension BLPlayerViewControllerTool{
    /// 配置IJKFFMoviePlayerController 信息
    func setupIJKplayerInfo() {
        if isDebug {
            IJKFFMoviePlayerController.setLogReport(true)
            IJKFFMoviePlayerController.setLogLevel(k_IJK_LOG_DEBUG)
        }else{
            
            IJKFFMoviePlayerController.setLogReport(false)
            IJKFFMoviePlayerController.setLogLevel(k_IJK_LOG_INFO)
        }
        IJKFFMoviePlayerController.checkIfFFmpegVersionMatch(true)

    }
}


//设置控制界面UI
extension BLPlayerViewControllerTool{
    
    ///设置控制界面
    func setupControlUI() {
        
    }
}



// MARK: - 设置播放的视频
extension BLPlayerViewControllerTool{
    
    func removePlay() {
        player?.shutdown()
        playerView?.removeFromSuperview()
        player = nil
    }
    
    func setPlayItem(urlStr : String) {
        setupIJKplayerInfo()
        
        // 是否应许视频播放
        guard isAllowPlay else {
            return
        }
        
        
        if player == nil{
            let options = IJKFFOptions.optionsByDefault()
            player = IJKFFMoviePlayerController(contentURLString: urlStr, withOptions: options)
            player?.view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth,UIViewAutoresizing.FlexibleHeight]
            player?.view.frame = view.bounds
            player?.scalingMode = IJKMPMovieScalingMode.AspectFit
            player?.shouldAutoplay = true
            
            view.autoresizesSubviews = true
            view.addSubview((player?.view)!)
            //设置控制界面
            setupControlUI()
            player?.prepareToPlay()
        }
        isPlay = true
    }
    

}


